package com.mfcxy.entityvo;

import com.mfcxy.exception.CustomException;
import com.mfcxy.service.IUserService;
import org.springframework.web.multipart.MultipartFile;

import com.mfcxy.annotations.ConfirmData;
import com.mfcxy.entity.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该实体类用于新增用户使用
 * 
 * @author mfcxy
 *
 */
@ConfirmData(field = "password", verifyField = "confirm_password", message = "二次密码不一致")
public class UserAddVo extends User {
	private String confirm_password;

	// 上传图片名称
	private MultipartFile image; // List<MultipartFile> images 多个图片

	public String getConfirm_password() {
		return confirm_password;
	}

	public void setConfirm_password(String confirm_password) {
		this.confirm_password = confirm_password;
	}

	public MultipartFile getImage() {
		return image;
	}

	public void setImage(MultipartFile image) {
		this.image = image;
	}

	/**
	 * 添加新用户验证方法
	 * 
	 * @param userService
	 */
	public void checkAdd(IUserService userService) throws CustomException {
		List<String> msglist = new ArrayList<>();
		if (getUsername() == null || getUsername().equals("")) {
			msglist.add("用户名不能为空");
		} else if (userService.checkUserName(getUsername())) {
			msglist.add("已经存在的用户名");
		}

		if (getPassword() == null || getPassword().length() < 6) {
			msglist.add("密码不能小于6位");
		}

		if (!getPassword().equals(getConfirm_password())) {
			msglist.add("二次密码不一致");
		}

		// 如果存在异常
		if (msglist.size() > 0) {
			throw new CustomException(msglist.toString());
		}
	}

	/**
	 * 更新用户资料方法
	 * 
	 * @param userService
	 */
	public void checkUpdate(IUserService userService) throws CustomException {
		List<String> msglist = new ArrayList<>();

		// sql 查询条件
		Map<String, Object> map = new HashMap<>();
		map.put("username", getUsername());
		map.put("id", getId());

		if (getUsername() == null || getUsername().equals("")) {
			msglist.add("用户名不能为空");
		} else if (userService.checkUserName(map)) {
			msglist.add("已经存在的用户名");
		}

		// 如果存在异常
		if (msglist.size() > 0) {
			throw new CustomException(msglist.toString());
		}
	}

	/**
	 * 重置密码验证
	 * 
	 * @throws CustomException
	 */
	public void checkRestPwd() throws CustomException {
		List<String> msglist = new ArrayList<>();
		if (getPassword() == null || getPassword().length() < 6) {
			msglist.add("密码不能小于6位");
		} else if (getConfirm_password() != null && !getPassword().equals(getConfirm_password())) {
			msglist.add("二次密码不相等");
		}

		// 如果存在异常
		if (msglist.size() > 0) {
			throw new CustomException(msglist.toString());
		}
	}

	/**
	 * 登陆数据效验
	 * @throws CustomException
	 */
	public void checkLogin() throws CustomException {
		List<String> msglist = new ArrayList<>();
		if (getUsername() == null || getUsername().equals("")) {
			msglist.add("用户名不能为空");
		}
		
		if(getPassword() == null || getPassword().equals("")) {
			msglist.add("密码不能为空");
		}

		// 如果存在异常
		if (msglist.size() > 0) {
			throw new CustomException(msglist.toString());
		}
	}

	@Override
	public String toString() {
		return "UserAddVo [confirm_password=" + confirm_password + ", image=" + image + ", getId()=" + getId()
				+ ", getUsername()=" + getUsername() + ", getPassword()=" + getPassword() + ", getHead_portrait()="
				+ getHead_portrait() + ", getCreatetime()=" + getCreatetime() + ", getUpdatetime()=" + getUpdatetime()
				+ "]";
	}

}
